<template>
  <div class='settings'>
    <div class="settings_main">
      <p>请先设置steam游戏库common路径:</p>
      <button @click="getSteamPath()">自动检测</button>
      <input type="text" placeholder="例如：D:/steam/steamapps/common" id="steamPath" v-model="folderPath">
      <button @click="selectFolder()">手动选择</button>
    </div>
  </div>
</template>

<script setup lang='ts'>
import { ref } from 'vue';
// import { enumerateValues, HKEY } from 'registry-js'


  const folderPath = ref<any>(null)

  folderPath.value = localStorage.getItem('steamPath') || ''

  async function selectFolder(){
    try{
      // 通过 IPC 调用主进程的对话框
      const selectFolder = await window.electron.ipcRenderer.invoke('open-folder-dialog');
      localStorage.setItem('steamPath',selectFolder)
      folderPath.value = selectFolder
      // resultPath.textContent = folderPath;
      
    } catch (error) {
        console.error('Error:', error);
        // resultPath.textContent = '错误: ' + error.message;
    }
  }

  async function getSteamPath(){
    const steamPath = await window.electron.ipcRenderer.invoke('getSteamPath')
    if(steamPath!=null){
      folderPath.value = steamPath
      localStorage.setItem('steamPath',steamPath)
    }
  }

/*   async function getSteamPath(){

    const values = enumerateValues(
      HKEY.HKEY_CURRENT_USER,
      'Software\\Valve\\Steam'
    )
    for(const value of values){
      if(value.name==='SteamPath'){
        const steamPath = value.data
        const commonPath = steamPath+'/steamapps/common'
        console.log(commonPath)
        return commonPath
      }
    }
    return null
  } */

  

</script>
<style scoped lang='scss'>
  .settings {
    position: relative;
    width: calc(100% - 80px);
    height: 100vh;
    float: right;
    position: relative;
    background-color: rgba($color: white, $alpha: 0.8);
    overflow-y: auto;
    overflow-x: hidden;
    text-align: center;
    .settings_main{
      margin: 20px 0;
    }
    p{
      margin: 5px auto;
    }
    input{
      width: 300px;
      height: 40px;
    }
    button{
      height: 40px;
      margin: 0 5px;
    }
  }
</style>
